<?php
/**
 * User: xzq
 * Date: 2018/8/9
 * Time: 11:17
 */

namespace app\library\admin;
use app\admin\controller\Admin;
use app\library\model\User as UserModel;
use app\library\model\Violation as ViolationModel;

class Violation extends Admin
{
    public function index()
    {
        $this->assign('tab_type', 3);
        if ($this->request->isAjax()) {
            $where = $data = [];
            $page = input('param.page/d', 1);
            $limit = input('param.limit/d', 15);
            $card_id = input('param.card_id');
            $name = input('param.name');
            $status = input('param.status');
            $stime = input('param.stime');
            $etime = input('param.etime');
            if (!empty($card_id)){
                $where['card_id'] = $card_id;
            }
            if (!empty($name)){
                $where['name'] = $name;
            }
            if (!empty($status) || $status === "0"){
                $where['v.status'] = $status;
            }
            if (!empty($stime) && !empty($etime) && $stime >= $etime){
                return ['code'=>0,'count'=>0,'data'=>[],'msg'=>'开始不能大于结束时间'];
            }
            if (!empty($stime) && !empty($etime)){
                $stime = strtotime($stime);
                $etime = strtotime($etime);
                $where['v.ctime'] = ['between',"$stime,$etime"];
            }elseif(!empty($stime)){
                $stime = strtotime($stime);
                $where['v.ctime'] = ['>=',$stime];
            }elseif(!empty($etime)){
                $etime = strtotime($etime);
                $where['v.ctime'] = ['<=',$etime];
            }
            $user = new UserModel();
            $list = ViolationModel::where($where)
                ->alias('v')
                ->field('v.id,card_id,name,reservationid,type,v.status,v.ctime')
                ->join('user u','v.userid = u.id')
                ->page($page)->limit($limit)->order('id desc')->select();

            foreach ($list as $key => $value) {
                switch ($value['type']) {
                    case '1': $typeName = '未签到';break;
                    case '2': $typeName = '未签退';break;
                    default:$typeName = '暂无';; break;
                }
                $list[$key]['type'] = $typeName;
                $list[$key]['ctime'] = date("Y-m-d H:i",$value['ctime']);
            }
            $data['data'] = $list;
            $data['count'] = ViolationModel::where($where)->alias('v')->join('user u','v.userid = u.id')->count('v.id');
            $data['code'] = 0;
            $data['msg'] = '';
            return json($data);
        }
        return $this->fetch();
    }
}